import java.util.ArrayList;
import lejos.nxt.comm.RConsole;


public class TrackDetector {

	int posx = 0;
    int posy = 0;
    int richting = 0;
    int panelNumber = 0;
    ArrayList<BarCode> track = new ArrayList<BarCode>();
    Master master;
    
    public TrackDetector(Master master){
    	this.master = master;
    	track.add(BarCode.RECHTDOOR);
    }
	
	//getter maken met modulo-index
    public BarCode getBarCodeAt(int panelNum){
            int panel = panelNum%(track.size());
            RConsole.println("Paneel "+panel +": "+ track.get(panel));
            return track.get(panel);
    }
    
    public void resetTrack(){
        panelNumber=0;
        richting=0;
        posx=0;
        posy=0;
        track.clear();
        track.add(BarCode.RECHTDOOR);
    }
    
    
    public void nextPanel(BarCode newBar){
        panelNumber++;
        //Richtingen: 0 = rechts
        // 1 = onder
        // 2 = links
        // 3 = boven
        if (richting == 0)
                posx++;
        else if (richting == 1)
                posy--;
        else if (richting == 2)
                posx--;         
        else if (richting == 3)
                posy++;

        if(newBar == BarCode.RECHTDOOR){
                //richting = richting;
        }
        else if (newBar == BarCode.LINKS){
                richting = (richting+3)%4;
        }
        else if (newBar == BarCode.RECHTS){
                richting = (richting+1)%4;
        }

        
        //System.out.println("posx: " + posx +" posy: " + posy);

        if(posx == 0 && posy == 0){
                master.setTrackCompleted(true);
                panelNumber = 0;
                track.set(0,newBar);
//                for(int i = 0; i<track.size(); i++){
//                	RConsole.println(""+track.get(i));
//                }
        }
        else{
                track.add(newBar);
        }
}
}
